const defaultState = {
    feeList: [],
    order: {}

};
export default function orderOutFee(state = defaultState, action) {
    const {type, payload} = action;
    let obj = null;
    switch (type) {
        case 'GH_ORDER.UPDATE_ORDER_OUT_FEE':
            if (payload && payload.length > 0) {
                payload.forEach(item => {
                    const found = state.feeList.find(f => f.feeName === item.feeName);
                    if (!found) {
                        state.feeList.push({feeName: item.feeName, status: "UNLOCK"});
                    }
                });
            }
            // 更新feeList
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = payload.find(f => f.feeName === fee.feeName);
                    if (found) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                        fee.carrierId = found.carrierId;
                        fee.canSee = found.canSee;
                        fee.status = found.status;
                    }
                });
            }
            const fees = [];
            state.feeList.forEach(fee => {
                fees.push({...fee, id: Math.random().toString()});
            });
            obj = null;
            // 把运输费排到最前面
            fees.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    fees.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                fees.unshift(obj);
            }
            return {...state, feeList: fees};
        case 'GH_ORDER.HIDE_EDIT':
        case 'GH_ORDER.HIDE_CREATE':
            return {...state, feeList: []};
        case 'GH_ORDER.UPDATE_MODEL':
            return {...state, order: {...payload}};
        case 'GH_ORDER.GET_FEE_LIST':
            const feeList = [];
            if (payload && payload.length > 0) {
                payload.forEach(item => feeList.push({
                    id: Math.random().toString(),
                    feeName: item.name,
                    feeCount: 0,
                    currency: undefined,
                    canSee: true,
                    carrierId: undefined
                }));
            }
            return {...state, feeList: [...feeList]};
        case 'GH_ORDER.UPDATE_OUT_FEE_LIST':
            return {...state, feeList: [...payload]};
        case 'GH_ORDER.CLEAR_FEE':
            const newFeeList = [];
            state.feeList.forEach(fee => {
                newFeeList.push({...fee, feeCount: 0, currency: undefined, id: Math.random().toString()});
            });
            return {
                ...state, feeList: [...newFeeList]
            };
        case 'GH_ORDER.EMPTY_CARRIER':
            const newFeeList2 = [];
            state.feeList.forEach(fee => {
                if (fee.feeName === payload) {
                    newFeeList2.push({...fee, carrierId: undefined, currency: undefined});
                } else {
                    newFeeList2.push({...fee, feeCount: 0, currency: undefined, id: Math.random().toString()});
                }
            });
            obj = null;
            // 把运输费排到最前面
            newFeeList2.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    newFeeList2.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                newFeeList2.unshift(obj);
            }
            return {
                ...state, feeList: [...newFeeList2]
            };
        case 'GH_ORDER.CLEAR_VEHICLE_FEE':
            if (payload) {
                const found = state.feeList.find(f => f.feeName === payload);
                if (found) {
                    found.feeCount = 0;
                    found.currency = undefined;
                }
            }
            const fees5 = [];
            state.feeList.forEach(fee => {
                fees5.push({...fee, id: Math.random().toString()});
            });
            obj = null;
            // 把运输费排到最前面
            fees5.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    fees5.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                fees5.unshift(obj);
            }
            return {...state, feeList: fees5};
        case 'GH_ORDER.CALCULATE_OUT_FEE':
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = payload.find(f => f.feeName === fee.feeName);
                    if (found) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                    }
                });
            }
            const fees2 = [];
            state.feeList.forEach(fee => {
                fees2.push({...fee, id: Math.random().toString()});
            });
            obj = null;
            // 把运输费排到最前面
            fees2.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    fees2.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                fees2.unshift(obj);
            }
            return {...state, feeList: [...fees2]};
        case 'GH_ORDER.CALCULATE_VEHICLE_FEE':
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = payload.find(f => f.feeName === fee.feeName);
                    if (found) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                    }
                });
            }
            const fees6 = [];
            state.feeList.forEach(fee => {
                fees6.push({...fee, id: Math.random().toString()});
            });
            obj = null;
            // 把运输费排到最前面
            fees6.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    fees6.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                fees6.unshift(obj);
            }
            return {...state, feeList: [...fees6]};
        default:
            return state;
    }
}

